package com.jzo2o.foundations.handler;

import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.HomeService;
import com.jzo2o.foundations.service.IRegionService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class SpringCacheSyncHandler {

    @Resource
    IRegionService regionService;
    @Resource
    HomeService homeService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    /**
     * 每天凌晨一点更新已开通的区域服务
     */
    @XxlJob("activeRegionCacheSync")
    public void activeRegionCacheCacheSync(){
        log.info("每天凌晨一点更新已开通的区域服务，开始之执行");
        //key
        String key = RedisConstants.CacheName.JZ_CACHE+"::ACTIVE_REGIONS";
        //删除旧的已开通的区域服务
        redisTemplate.delete(key);
        //缓存进去
        List<RegionSimpleResDTO> regionSimpleResDTOS = regionService.queryActiveRegionListCache();

        //更新所有区域的热点数据
        regionSimpleResDTOS.forEach(regionDTO ->{
            homeService.queryHotServeList(regionDTO.getId());
        } );
    }
}
